#include "Enemy.h"
#include "ResourceManager.h"

CEnemy::CEnemy(D3DXVECTOR2 pos) : CObject()
{
	m_pos = pos;
	m_direct = 1;
	m_velocity.x = VELOCITY_X;
	m_status = ENEMY_ON_AIR;
}

void CEnemy::Init()
{
	m_sprite = CResourceManager::GetInstance()->GetSprite(RESOURCE__GAMEPLAY_ENEMY);
	CObject::Init();
}

void CEnemy::Update(float gameTime, CInput* input)
{
	// Xu ly input to gia toc va van toc
	//m_acceleration.y = -ACCELERATION_Y;
	//m_velocity.y = VELOCITY_JUMP;
	//m_timeJump += gameTime;
	//if (m_timeJump >= m_timeJumpMax)
	//{
	//	//m_status = MARIO_JUMP;
	//	m_acceleration.y = ACCELERATION_Y;
	//	m_velocity.y = -VELOCITY_JUMP;
	//	m_timeJump = 0.0f;
	//	m_timeJumpMax = (rand() % 30)/10.0f + 1;
	//}
	if (m_pos.x > 1950)
	{
		m_pos.x = 1950;
		m_velocity.x = -m_velocity.x;
		m_direct = -m_direct;
	} else if (m_pos.x < 0)
	{
		m_pos.x = 0;
		m_velocity.x = -m_velocity.x;
		m_direct = -m_direct;
	}

	// Xu ly status thanh animate
	UpdateAnimation(gameTime, input);

	// Phai co dc van toc va gia toc
	CObject::UpdatePosition(gameTime, input); // Update vi tri
}

void CEnemy::UpdateAnimation(float gameTime, CInput* input)
{

	m_sprite->SetTimeAnimation(0.1f);
	m_sprite->Update(gameTime, 0, 1);
}

void CEnemy::Draw(LPD3DXSPRITE spriteHandler, CCamera* camera)
{
	D3DXMATRIX temp, matrix, temp2;
	spriteHandler->GetTransform(&temp);

	D3DXMatrixTransformation2D(&matrix, &D3DXVECTOR2(m_sprite->GetFrameWidth()/2, m_sprite->GetFrameHeight()/2), 0.0f, &D3DXVECTOR2((float)m_direct, 1.0f), NULL, 0.0f, &D3DXVECTOR2(0,0));

	D3DXMatrixMultiply(&matrix, &temp, &matrix);
	spriteHandler->SetTransform(&matrix);
	CObject::Draw(spriteHandler, camera);
	spriteHandler->SetTransform(&temp);
}